home *** CD-ROM | disk | FTP | other *** search
/ PC Direct 1998 August / PC Direct August 1998.iso / S / powerj / Product / hpp.z / WMEMORY.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-25  |  8.3 KB  |  263 lines

  1. /*************************************************************************
  2.  *
  3.  * WMemory 
  4.  *
  5.  *************************************************************************/
  6.  
  7. #ifndef _WMEMORY_HPP_INCLUDED
  8. #define _WMEMORY_HPP_INCLUDED
  9.  
  10. #ifndef _WNO_PRAGMA_PUSH
  11. #pragma pack(push,8);
  12. #pragma enum int;
  13. #endif
  14.  
  15. #ifndef _WDEF_HPP_INCLUDED
  16. #  include "wdef.hpp"
  17. #endif
  18. #ifndef _WLLIST_HPP_INCLUDED
  19. #  include "wllist.hpp"
  20. #endif
  21.  
  22. #include <new.h>
  23.  
  24. //
  25. // Memory tracking flags
  26. //
  27.  
  28. #define WMEMORY_CHECKFOROVERWRITE 0x00000001L
  29. #define WMEMORY_TRACKMEMORY       0x00000002L
  30. #define WMEMORY_TRAPNOMEMORY      0x00000004L
  31. #define WMEMORY_WALKHEAPS         0x00000008L
  32. #define WMEMORY_USE_HEAPALLOC     0x00000010L
  33. #define WMEMORY_PRINT_ALL         0x80000000L
  34. #define WMEMORY_ZERO_MEMORY       0x40000000L
  35.  
  36. #define WMEMORY_ALLCHECKS         (WMEMORY_CHECKFOROVERWRITE|\
  37.                                    WMEMORY_TRACKMEMORY|\
  38.                                    WMEMORY_WALKHEAPS)
  39.  
  40. #define WMEMORY_NOCHECKS          0x00000000L
  41.  
  42. //
  43. // WMemoryAllocator -- Use this structure to define the low-level
  44. //                     C-type functions used to allocate memory.
  45. //                     You must declare the structure statically.
  46. //
  47.  
  48. typedef void * WHeapHandle;
  49.  
  50. typedef void * __stdcall (*WHeapAllocator)( WHeapHandle heap, WULong memSize );
  51. typedef void * __stdcall (*WHeapReallocator)( WHeapHandle heap, void *mem,
  52.                                               WULong newSize );
  53. typedef WBool  __stdcall (*WHeapDeallocator)( WHeapHandle heap, void *mem );
  54. typedef WBool  __stdcall (*WHeapValidator)( WHeapHandle heap, void *mem );
  55.  
  56. typedef WHeapHandle __stdcall (*WHeapCreator)( WULong initialSize,
  57.                                                WULong maximumSize );
  58. typedef WBool       __stdcall (*WHeapDestructor)( WHeapHandle heap );
  59.  
  60. struct WCMCLASS WMemoryHeap {
  61.     wllist_link       link;
  62.     const WChar      *heapName;
  63.     WHeapHandle       heap;
  64.     WULong            initialSize;
  65.     WULong            maximumSize;
  66.  
  67.     WHeapAllocator    allocate;
  68.     WHeapDeallocator  deallocate;
  69.     WHeapReallocator  reallocate;
  70.     WHeapValidator    validate;
  71.     WHeapCreator      create;
  72.     WHeapDestructor   destruct;
  73. };
  74.  
  75. //
  76. // Memory dumping -- for debugging purposes.
  77. //
  78.  
  79. typedef WBool WCMDEF (*WMemoryDumper)( void *memory,
  80.                                         const WChar **memoryType,
  81.                                         WChar *buffer, WULong len );
  82.  
  83. #ifdef _DEBUG
  84. #define WAttachDumper(mem,dump) WMemory::AttachDumpRoutine((void*)mem,dump)
  85. #else
  86. #define WAttachDumper(mem,dump)
  87. #endif
  88.  
  89. //
  90. // WMemory
  91. //
  92.  
  93. class WCMCLASS WMemory {
  94.  
  95.     public:
  96.  
  97.         /**********************************************************
  98.          * Constructors and Destructors
  99.          *********************************************************/
  100.  
  101.         WMemory();
  102.  
  103.         virtual ~WMemory();
  104.  
  105.         /**********************************************************
  106.          * Static properties
  107.          *********************************************************/
  108.  
  109.         // ArrayHeap
  110.  
  111.         static WMemoryHeap *GetArrayHeap();
  112.         static WBool        SetArrayHeap( WMemoryHeap *heap );
  113.  
  114.         // CurrentMemoryUsage
  115.  
  116.         static WULong GetCurrentMemoryUsage();
  117.  
  118.         // GlobalNewHandler
  119.         //
  120.         //    Sets a global new_handler value for the library.
  121.  
  122.         static PFV SetGlobalNewHandler( PFV new_handler );
  123.  
  124.         // Heap
  125.  
  126.         static WMemoryHeap *GetHeap();
  127.         static WBool        SetHeap( WMemoryHeap *heap );
  128.  
  129.         // NewHandler
  130.         //
  131.         //    Sets the C++ new_handler value for the library.
  132.         //    Returns the old new_handler.  You should call this
  133.         //    if you call set_new_handler in your application
  134.         //    to make sure that the library is using the same
  135.         //    new_handler.
  136.  
  137.         static PFV SetNewHandler( PFV new_handler );
  138.     
  139.         // PeakMemoryUsage
  140.  
  141.         static WULong GetPeakMemoryUsage();
  142.     
  143.         /**********************************************************
  144.          * Static methods
  145.          *********************************************************/
  146.  
  147.         // Allocate
  148.         //
  149.         //     Allocate memory from a given heap.  If no heap
  150.         //     specified, use the thread default.
  151.  
  152.         static void  *Allocate( WULong size, WBool array,
  153.                                 WMemoryHeap *allocator=NULL );
  154.         static void  *Allocate( WULong size, WBool array,
  155.                                 const WChar *functionName,
  156.                                 const WChar *fileName, WULong line,
  157.                                 WMemoryHeap *allocator=NULL );
  158.  
  159.         // AttachDumpRoutine
  160.         //
  161.         //     After a piece of memory has been allocated, call
  162.         //     this routine to attach a specific dump routine to it.
  163.  
  164.         static WBool  AttachDumpRoutine( void *memory,
  165.                                          WMemoryDumper dumpRoutine );
  166.  
  167.         // Deallocate
  168.         //
  169.         //     Deallocate a specific piece of memory.
  170.  
  171.         static WBool  Deallocate( void *memory, WBool array );
  172.         static WBool  Deallocate( void *memory, WBool array,
  173.                                   const WChar *functionName,
  174.                                   const WChar *fileName, WULong line );
  175.  
  176.         // Reallocate
  177.         //
  178.         //     Reallocate a specific piece of memory.
  179.  
  180.         static void  *Reallocate( void *memory, WULong newSize,
  181.                                   WBool array );
  182.         static void  *Reallocate( void *memory, WULong newSize,
  183.                                   WBool array,
  184.                                   const WChar *functionName,
  185.                                   const WChar *fileName, WULong line );
  186.  
  187.         // Validate
  188.         //
  189.         //     Check to see if a piece of memory is valid.  If NULL
  190.         //     is passed, checks whole heap.  Only implemented in
  191.         //     the debugging DLL.
  192.  
  193.         static WBool Validate( void *memory, WMemoryHeap *allocator );
  194.  
  195.         /**********************************************************
  196.          * Internal
  197.          *********************************************************/
  198.  
  199.         // LogNextDelete
  200.         //
  201.         //     Set the location that the next delete will come from.
  202.         //     Used in debugging mode with memory tracking.
  203.  
  204.         static WBool LogNextDelete( void *ptr, const WChar *functionName,
  205.                                     const WChar *fileName, WULong line );
  206.  
  207.         // FreeDeleteLog
  208.         //
  209.         //     Free the delete log for a thread.  Call occasionally.
  210.  
  211.         static WBool FreeDeleteLog();
  212.  
  213.         /**********************************************************
  214.          * Data members
  215.          *********************************************************/
  216.  
  217.     public:
  218. };
  219.  
  220. //
  221. // Use these macros to redefine new & delete
  222. //
  223.  
  224. #ifndef _NO_REDEFINE_NEW
  225.  
  226. #define WREDEFINENEW \
  227.     void *operator new( size_t size ) \
  228.                      { return WMemory::Allocate( size, FALSE ); } \
  229.     void *operator new( size_t size, const WChar *funcName, \
  230.                         const WChar *fileName, WULong line ) \
  231.                      { return WMemory::Allocate( size, FALSE, funcName, \
  232.                                                  fileName, line ); } \
  233.     void *operator new( size_t, void *p ) { return p; } \
  234.     void *operator new []( size_t size ) \
  235.                      { return WMemory::Allocate( size, TRUE ); } \
  236.     void *operator new []( size_t size, const WChar *funcName,\
  237.                            const WChar *fileName, WULong line ) \
  238.                      { return WMemory::Allocate( size, TRUE, funcName,\
  239.                                                  fileName, line ); } \
  240.     void *operator new []( size_t, void *p ) { return p; }
  241.  
  242. #define WREDEFINEDELETE \
  243.     void operator delete( void *p ) { WMemory::Deallocate( p, FALSE ); } \
  244.     void operator delete []( void *p ) { WMemory::Deallocate( p, TRUE ); }
  245.  
  246. #else
  247.  
  248. #define WREDEFINENEW
  249. #define WREDEFINEDELETE
  250.  
  251. #endif
  252.  
  253. #define W_REDEFINENEW    WREDEFINENEW
  254. #define W_REDEFINEDELETE WREDEFINEDELETE
  255.  
  256. #ifndef _WNO_PRAGMA_PUSH
  257. #pragma enum pop;
  258. #pragma pack(pop);
  259. #endif
  260.  
  261. #endif // _WMEMORY_HPP_INCLUDED
  262.  
  263.